package com.wgl.yygh.controller.api;

import com.wgl.model.hosp.Schedule;
import com.wgl.model.hosp.ScheduleQueryVo;
import com.wgl.yygh.serivce.ScheduleService;
import org.springframework.data.domain.Page;
import com.wgl.model.hosp.Department;
import com.wgl.model.hosp.Hospital;
import com.wgl.model.vo.hosp.DepartmentQueryVo;
import com.wgl.yygh.common.helper.HttpRequestHelper;
import com.wgl.yygh.common.util.MD5;
import com.wgl.yygh.exception.HospitalException;
import com.wgl.yygh.result.Result;
import com.wgl.yygh.result.ResultCodeEnum;
import com.wgl.yygh.serivce.DepartmentService;
import com.wgl.yygh.serivce.HospitalService;
import com.wgl.yygh.serivce.HospitalSetService;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName ApiControeller.java
 * @Description TODO
 * @createTime 2022年02月05日 21:44:00
 */
@RestController
@RequestMapping("/api/hosp")
@CrossOrigin
public class ApiController {
    @Resource
    private HospitalService hospitalService;
    @Resource
    private HospitalSetService hospitalSetService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private ScheduleService scheduleService;


    /**
     * @Author: Wgl
     * @Description: 查询排班
     * @Date: 2022/2/6 14:16;
     * @param request: ;
     * @return: com.wgl.yygh.result.Result;
     */
    @PostMapping("/schedule/remove")
    public Result scheduleRemove(HttpServletRequest request){

        Map<String, Object> map = convertMap(request.getParameterMap());

        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");


        //校验key是否正确
        check(sign,hospcode);

        //得到传递过来的科室的编号
        String hospScheduleId = (String)map.get("hosScheduleId");

        scheduleService.remove(hospcode,hospScheduleId);

        return Result.ok();
    }



    /**
     * @Author: Wgl
     * @Description: 查询排班
     * @Date: 2022/2/6 14:16;
     * @param request: ;
     * @return: com.wgl.yygh.result.Result;
     */
    @PostMapping("/schedule/list")
    public Result scheduleList(HttpServletRequest request){
        Map<String, Object> map = convertMap(request.getParameterMap());
        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");


        //校验key是否正确
        check(sign,hospcode);

        //得到传递过来的科室的编号
        String depcode = (String)map.get("depcode");

        String s  = null;
        Integer page = (s = (String) map.get("page")) == null ? 1 : Integer.parseInt(s);
        s = null;
        Integer limit = (s = (String) map.get("limit")) == null ? 3 : Integer.parseInt(s);

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hospcode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> page1 = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);

        return Result.ok(page1);
    }


    /**
     * @Author: Wgl
     * @Description: 上传排班
     * @Date: 2022/2/6 14:16;
     * @param request: ;
     * @return: com.wgl.yygh.result.Result;
     */
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> map = convertMap(request.getParameterMap());
        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");


        //校验key是否正确
        check(sign,hospcode);

        scheduleService.save(map);

        return Result.ok();
    }


    /**
    * @Author: Wgl
    * @Description:删除科室
    * @Date: 2022/2/6 15:21;
    * @param request: ;
    * @return: com.wgl.yygh.result.Result;
    */
    @PostMapping("/department/remove")
    public  Result departmentRemove(HttpServletRequest request){
        Map<String, Object> map = convertMap(request.getParameterMap());
        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");

        //校验key是否正确
        check(sign,hospcode);

        //得到传递过来的科室的编号
        String depcode = (String)map.get("depcode");

        departmentService.remove(hospcode,depcode);
        return Result.ok();


    }
    /**
    * @Author: Wgl
    * @Description: 查询科室
    * @Date: 2022/2/6 14:45;
    * @param request: ;
    * @return: com.wgl.yygh.result.Result;
    */
    @PostMapping("/department/list")
    public Result departmentList(HttpServletRequest request){
        Map<String, Object> map = convertMap(request.getParameterMap());
        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");

        //校验key是否正确
        check(sign,hospcode);

        //初始化分页参数  带有当前则已带的为主 没带默认为1
        String s  = null;
        Integer page = (s = (String) map.get("page")) == null ? 1 : Integer.parseInt(s);
        s = null;
        Integer limit = (s = (String) map.get("limit")) == null ? 3 : Integer.parseInt(s);

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hospcode);
        Page<Department> page1 = departmentService.findPageDepartment(page,limit,departmentQueryVo);

        return Result.ok(page1);
    }
    /**
    * @Author: Wgl
    * @Description: 上传科室
    * @Date: 2022/2/6 14:16;
    * @param request: ;
    * @return: com.wgl.yygh.result.Result;
    */
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> map = convertMap(request.getParameterMap());
        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");


        //校验key是否正确
        check(sign,hospcode);

        departmentService.save(map);

        return Result.ok();
    }


    /**
    * @Author: Wgl
    * @Description: 查询医院
    * @Date: 2022/2/6 13:48;
    * @param request: ;
    * @return: com.wgl.yygh.result.Result;
    */
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        Map<String, Object> map = convertMap(request.getParameterMap());
        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");

        //校验key是否正确
        check(sign,hospcode);

        Hospital hospital =  hospitalService.getByHoscode(hospcode);

        return Result.ok(hospital);
    }
    /**
    * @Author: Wgl
    * @Description:上传医院
    * @Date: 2022/2/6 14:13;
    * @param request: ;
    * @return: com.wgl.yygh.result.Result;
    */
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String, Object> map = convertMap(request.getParameterMap());

        //获取医院的key 是经过MD5加密的
        String sign = (String)map.get("sign");
        //得到传递过来的医院的编号
        String hospcode = (String)map.get("hoscode");
        //校验key是否正确
        check(sign,hospcode);

        //logoData为图片的编码 传输中将其中的+变为了 空格  现在要将 空格转化为+
        String logoData = ((String) map.get("logoData")).replace(" ","+");
        map.put("logoData",logoData);
        hospitalService.save(map);
        return Result.ok();
    }
    /**
    * @Author: Wgl
    * @Description: 校验key
    * @Date: 2022/2/6 14:47;
    * @param sign:
     * @param hospcode: ;
    * @return: void;
    */
    private void  check(String sign,String hospcode){
        //通过yygh_hosp数据库表中的hospcode得到对应医院的key
        String signKey = hospitalSetService.getSignKey(hospcode);
        String encrypt = MD5.encrypt(signKey);
        if (!sign.equals(encrypt)){
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
    }
    /**
    * @Author: Wgl
    * @Description: 转换Map
    * @Date: 2022/2/6 15:24;
    * @param parameterMap: ;
    * @return: java.util.Map<java.lang.String,java.lang.Object>;
    */
    private Map<String,Object> convertMap(Map<String, String[]> parameterMap){

        return HttpRequestHelper.switchMap(parameterMap);
    }
}
